<template>
  <div class="to-examine">
    <el-collapse v-model="activeNames" class="custom-collapse">
      <el-collapse-item name="1">
        <template slot="title">
          <div class="title search-title">精准查找</div>
        </template>
        <div class="search">
          <div class="first">
            <div class="input">
              <label>商户编号：</label>
              <el-input v-model.trim="params.search.merchant_no" clearable></el-input>
            </div>
            <div class="input">
              <label>展示名：</label>
              <el-input v-model.trim="params.search.merchant_name" clearable></el-input>
            </div>
          </div>
          <div class="first">
            <div class="input">
              <label>法人名称：</label>
              <el-input v-model.trim="params.search.lp_name" clearable></el-input>
            </div>
            <div class="input">
              <label>签约名：</label>
              <el-input v-model.trim="params.search.sign_name" clearable></el-input>
            </div>
          </div>
          <div class="first">
            <div class="input">
              <label>状态查找：</label>
              <el-select
                v-model="params.audit_status"
                placeholder="请选择"
                clearable
                @change="statusChange"
              >
                <el-option
                  v-for="item in statusList"
                  :key="item.status"
                  :label="item.label"
                  :value="item.status"
                ></el-option>
              </el-select>
            </div>
            <div class="input" style="max-width:480px">
              <label style="min-width: 22%;">申请时间：</label>
              <el-date-picker
                v-model="time"
                style="width:500px!important"
                type="datetimerange"
                range-separator="到"
                start-placeholder="申请开始时间"
                end-placeholder="申请结束时间"
              ></el-date-picker>
            </div>
          </div>
          <div class="btn">
            <el-button
              type="primary"
              @click="search()"
              v-if="searchInterface('merchant_applies.management')"
            >查找</el-button>
            <el-button @click="resetParams()">重置</el-button>
          </div>
        </div>
      </el-collapse-item>
    </el-collapse>
    <div class="content">
      <examineTable
        :loading="loading"
        :list="examineList"
        :callback="childCallback"
        :status="params.audit_status"
      />
      <div class="footer">
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="params.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="params.per_limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
// 微卡商户审核-商户审核
import examineTable from './component/examine/examine-table'
import { merchantApplies } from '@/api/merchant-examine/examine'
import { date, copy } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  components: {
    examineTable
  },
  computed: {
    ...mapGetters({
      isExamine: 'getExamineID'
    })
  },
  watch: {
    isExamine(value) {
      // 进入审核详情页，如果已经审核，则删除掉本条审核的数据
      this.childCallback(value)
    }
  },
  data() {
    return {
      activeNames: ['1'],
      examineList: [],
      loading: false,
      time: [],
      total: 0,
      params: {
        page: 1,
        per_limit: 10,
        audit_status: '',
        search: {
          sign_name: '',
          merchant_no: '',
          merchant_name: '',
          lp_name: '',
          apply_time_begin: '',
          apply_time_end: ''
        }
      },
      statusList: [
        {
          label: '待审核',
          status: 0
        },
        {
          label: '审核成功',
          status: 1
        },
        {
          label: '审核失败',
          status: 2
        }
      ]
    }
  },
  beforeMount() {
    this.getData()
  },
  methods: {
    // 查询
    search() {
      this.getData()
    },
    // 状态切换
    statusChange() {
      this.getData()
    },
    // 监听子组件回调
    childCallback(id) {
      const index = this.examineList.findIndex(i => i.id === id)
      this.examineList.splice(index, 1)
    },
    // 重置查找条件
    resetParams() {
      this.time = []
      this.params = {
        page: 1,
        per_limit: 15,
        audit_status: '',
        search: {
          sign_name: '',
          merchant_no: '',
          merchant_name: '',
          lp_name: '',
          apply_time_begin: '',
          apply_time_end: ''
        }
      }
      this.getData()
    },
    // 获取
    getData() {
      this.loading = true
      let params = copy.deepCopy(this.params)
      if (this.time && this.time.length) {
        const [begin, end] = this.time
        params.search.apply_time_begin = begin.getTime() / 1000
        params.search.apply_time_end = end.getTime() / 1000
      }
      merchantApplies(params)
        .then(res => {
          const {
            data,
            meta: {
              pagination: { total }
            }
          } = res
          data.forEach(i => {
            i.audited_at = date.formatDate(i.audited_at)
            i.created_at = date.formatDate(i.created_at)
          })
          this.examineList = data
          this.total = total
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 切换页码
    currentChange(value) {
      this.params.page = value
      this.getData()
    },
    sizeChange(value) {
      this.params.per_limit = value
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
.to-examine {
  width: 100%;
  height: 100%;
}
</style>